#include "widget_buy.h"
#include "units.h"
#include "players.h"



buy_widget::buy_widget(QWidget* parent,Qt::WindowFlags flags)
{
	current_player_num = 0;
	layout = new QVBoxLayout( this );


	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );
	
	QLabel* title_l = new QLabel( title_w );
	title_l->setTextFormat( Qt::RichText );
	title_l->setText( "<b><font size=8>Players settings</font></b>" );
	title_w_l->addWidget( title_l );

	next_b = new QPushButton( "NEXT", title_w );
	next_b->setFixedWidth( 150 );
	title_w_l->addWidget( next_b );
	connect( next_b, SIGNAL( clicked() ), this, SLOT( next() ) );

	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );


	current_widget = create_players_widget();
	layout->addWidget( current_widget );

	this->setLayout( layout );
};



QWidget* buy_widget::create_players_widget()
{
	current_player = players::get_instance()->get_player( current_player_num );
	QWidget* unknown = 0;
	switch ( current_player->get_type() )
	{
	case ai:
		ai_w = new ai_widget( current_player, next_b, this );
		return ai_w;
		break;
	case network:
		network_w = new network_widget( current_player, next_b, this );
		return network_w;
		break;
	case local:
		human_w = new human_widget( current_player, next_b, this );
		return human_w;
		break;
	default:
		return unknown;
		break;
	}
};



void buy_widget::next()
{
	++current_player_num;
	if ( current_player_num < players::get_instance()->get_players_count() )
	{
		delete current_widget;
		current_widget = create_players_widget();
		layout->addWidget( current_widget );
	}
	else
	{
		game_gui* ggui = game_gui::get_instance();
		MyQScrollArea* sa = new MyQScrollArea( ggui->g );
		ggui->g->play_w = new play_widget( sa );
		sa->setSizeHint( ggui->g->play_w->size() );
		sa->setWidget( ggui->g->play_w );
		sa->setWidgetResizable( true );
		ggui->g->setCentralWidget( sa );
		game::get_instance()->start_game();
	}
};